import "package:flutter/material.dart";
import 'package:flutter_pink_app/style/text.dart';
import 'package:flutter_pink_app/util.dart';

// 动态展示栏组合
class DynamicShowRow extends StatelessWidget {
  final List<String> contentList; // 展示内容
  final ValueChanged<int> onClickCardCallback; // 点击卡片的回调函数
  final TextStyle textStyle; // 文字样式
  final double cardHeight; // 卡片高度
  final BoxDecoration cardDecoration; // 卡片装饰效果
  final Alignment contentAlign;// 内容对齐情况

  const DynamicShowRow(
      {Key key,
      this.contentList = const [],
      this.cardDecoration,
      this.contentAlign = Alignment.center,
      this.textStyle = normalWhiteStyle,
      this.cardHeight,
      this.onClickCardCallback})
      : super(key: key); // 点击卡片触发对应索引的回调函数

  // 构建项目方法
  InkWell _buildItem(int index, double width, double height) {
    return InkWell(
      onTap: () {
        if (onClickCardCallback != null) onClickCardCallback(index);
      },
      child: Ink(
        height: height,
        width: width,
        decoration: cardDecoration,
        child: Align(
          alignment: contentAlign,
          child: Text(contentList[index], style: textStyle),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (_, limits) {
      final widthFactor = contentList.length != 0
          ? doubleDownToPrecision(1 / contentList.length, 1)
          : 1; // 计算宽比，需要保护长度为0的情况
      final items = <Widget>[];
      for (int i = 0; i < contentList.length; i++) {
        items.add(_buildItem(
            i, limits.maxWidth * widthFactor, cardHeight ?? limits.maxHeight));
      }
      return Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: items,
      );
    });
  }
}
